<template>
	<view class="followPage">
		<view class="followWrap">
			<view class="followItemWrap" v-for="(follow,index) in followConfig.list">
				<view class="followItem" @click.stop="navTo('/pages/home/artistDetail',{id:follow.user.id})">
					<view class="avatarWrap">
						<u-avatar :src="(follow.user&&follow.user.cover) || require('../../static/images/common/defaultAvatar.png')" size="70" mode="aspectFill"></u-avatar>
						<image class="authIcon" v-if="follow.user.authLevel==1" src="@/static/images/common/pre_icon.png" mode="widthFix" :style="{width:`50rpx`,height:`50rpx`,backgroundColor:`#fff`,borderRadius:`50%`}"></image>
					</view>	
					<view class="nickName text textHide">{{follow.user.nickname}}</view>
					<view class="time text">2021年10月1日</view>
					<view class="number text">{{follow.user.fansCount}}</view>
					<view class="fans text">粉丝</view>
					<view class="follow" @click.stop="_cancelFollow(follow,index)">取消关注</view>
				</view>
			</view>
		</view>
		<u-empty :show="followConfig.total == 0 && followConfig.list.length==0" mode="list" icon="http://cdn.uviewui.com/uview/empty/list.png" />
		<template v-if="followConfig.total != 0">
			<u-loadmore :status="followConfig.loadStatus" />
		</template>
	</view>	
</template>

<script>
	import {mapActions} from 'vuex';
	export default {
		data() {
			return {
				followConfig:{
					list: [],
					page: 1,
					pages: 0,
					limit: 10,
					loading: false,
					loadStatus: 'loadmore',
				}
			};
		},
		onLoad(){
			this._queryFollowList();
		},
		onReachBottom() {
			this._queryFollowList(true);
		},
		onPullDownRefresh(){
			uni.startPullDownRefresh();
			this.initListConfig();
			this._queryFollowList();
		},
		methods:{
			...mapActions('user',['queryFollowList']),
			...mapActions('follow',['cancelFollow']),
			initListConfig() {
				this.followConfig.page = 1;
				this.followConfig.total = -1;
				this.followConfig.pages = 0;
				this.followConfig.list = [];
				this.followConfig.loading = false;
				this.followConfig.loadStatus = "loadmore";
			},
			_queryFollowList(ReachBtm ){
				if (this.followConfig.loading || this.followConfig.loadStatus == "nomore") return false;
				this.followConfig.loading = true;
				this.followConfig.loadStatus = "loading";
				if (ReachBtm) this.followConfig.page++;
				this.queryFollowList({
					limit: this.followConfig.limit,
					page: this.followConfig.page,
				}).then(res => {
					res.rows.map(item=>{item.loading=false});
					this.followConfig.list = this.followConfig.list.concat(res.rows);
					this.followConfig.pages = res.pages;
					this.followConfig.total = res.total;
					this.followConfig.loadStatus = this.followConfig.page >= this.followConfig.pages ? "nomore" : "loadmore";
				}).finally(() => {
					this.followConfig.loading = false;
					uni.stopPullDownRefresh();
				})
			},
			_cancelFollow(item,index){
				if(this.followConfig.list[index].loading) return;
				this.followConfig.list[index].loading = true;
				this.cancelFollow({
					id: item.user.id,
				}).then(res=>{
					this.followConfig.list.splice(index,1);
					uni.$u.toast("取消关注");
				}).finally(()=>{
					this.followConfig.list[index].loading = false;
				});
			}
		}
	}
</script>

<style lang="scss">
.followPage{
	padding: 15rpx;
	.followWrap{
		display: flex;
		flex-direction:row;
		flex-wrap: wrap;
		.followItemWrap{
			padding: 15rpx;
			width: 50%;
			box-sizing: border-box;
			.followItem{
				height: 550rpx;
				width: 100%;
				border-radius: 10rpx;
				border:1px solid rgba(200, 200, 200, 0.3);
				background-color: #0a0a22;
				display: flex;
				flex-direction:column;
				justify-content: center;
				align-items: center;
				.avatarWrap{
					position: relative;
					margin-bottom: 20rpx;
					.authIcon{
						position: absolute;
						bottom: -10rpx;
						right: -10rpx;
					}
				}
				
				.text{
					margin-bottom: 10rpx;
					color: #999;
					font-size: 26rpx;
				}
				.time{
					font-size: 24rpx;
				}
				.number{
					margin-top:20rpx;
				}
				.nickName{
					color: #fff;
					font-weight: bold;
					width: 100%;
					padding: 0 30rpx;
					box-sizing: border-box;
					text-align: center;
				}
				.follow {
					color: #bf8fef;
					border: 2rpx solid #bf8fef;
					width: 75%;
					margin: 30rpx auto 0;
					padding: 12rpx 0;
					border-radius: 50rpx;
					font-size: 24rpx;
					font-weight: bold;
					text-align: center;
				}
				.cancel {
					color: #999999;
					border: 2rpx solid #999999;
				}
			}
		}
	}
}

</style>
